import { useMemo, useCallback, useState } from 'react';
import { getBaseUrl } from '@/services/urlconst';
import { getTenantID, requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { loadMdicMap } from '@/services/BaseLayout';
import { getVersionManageSearchColumns } from '../mconfig';
import { isThemeOpen } from '../../ThemeController/service';
import { VersionManageAPI } from '../common/type';

let baseUrl = getBaseUrl();
// let baseUrl = 'http://192.168.3.6:9090'; //少勇本地192.168.3.6
/**
 * 版本管理业务逻辑
 * @param curRes
 */
const VersionManageService = (curRes: Record<string, any>) => {
  /** 通过各类型获取各字典信息 */
  const loadMdicMapList = useRequest(() => {
    return loadMdicMap([
      `${getTenantID()}1201,${getTenantID()}1202,${getTenantID()}1203`,
    ]);
  });

  /**保存搜索参数 */
  const [manageTableParams, setManageTableParams] = useState<any>({});
  const tableParamsCallback = useCallback(
    (parObj: any) => {
      setManageTableParams(parObj);
    },
    [manageTableParams],
  );

  /** 表格数据请求处理业务 */
  const { tableProps, data, loading, run } = useRequest(
    (requestParams: VersionManageAPI.IVersionTableRequestType) => {
      const dataGridURL = `${baseUrl}/versionController/dataGrid`;
      let postParams = {
        ...requestParams,
        page: requestParams?.current ?? 1,
        rows: requestParams?.pageSize ?? 10,
      };
      tableParamsCallback(postParams);
      return requestFunComponFiex('POST', dataGridURL, postParams);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /** 重新请求数据 */
  const refreshRequest = useCallback(
    (requestParams: VersionManageAPI.IVersionTableRequestType | any) => {
      if (requestParams?.createTime) {
        const [createTimeStart, createTimeEnd] = requestParams.createTime;
        requestParams['createTimeStart'] = createTimeStart.format(
          'YYYY-MM-DD HH:mm:ss',
        );
        requestParams['createTimeEnd'] = createTimeEnd.format(
          'YYYY-MM-DD HH:mm:ss',
        );
        delete requestParams['createTime'];
      }

      if (requestParams?.signTime) {
        const [signTimeStart, signTimeEnd] = requestParams.signTime;
        requestParams['signTimeStart'] = signTimeStart.format(
          'YYYY-MM-DD HH:mm:ss',
        );
        requestParams['signTimeEnd'] = signTimeEnd.format(
          'YYYY-MM-DD HH:mm:ss',
        );
        delete requestParams['signTime'];
      }

      if (requestParams?.cancelTime) {
        const [cancelTimeStart, cancelTimeEnd] = requestParams.cancelTime;
        requestParams['cancelTimeStart'] = cancelTimeStart.format(
          'YYYY-MM-DD HH:mm:ss',
        );
        requestParams['cancelTimeEnd'] = cancelTimeEnd.format(
          'YYYY-MM-DD HH:mm:ss',
        );
        delete requestParams['cancelTime'];
      }

      if (requestParams?.applyTime) {
        const [applyTimeStart, applyTimeEnd] = requestParams.applyTime;
        requestParams['applyTimeStart'] = applyTimeStart.format(
          'YYYY-MM-DD HH:mm:ss',
        );
        requestParams['applyTimeEnd'] = applyTimeEnd.format(
          'YYYY-MM-DD HH:mm:ss',
        );
        delete requestParams['applyTime'];
      }

      // delete requestParams['createderFormNameDept'];
      // delete requestParams['createderFormNameUser'];
      delete requestParams['createFormNameDept'];
      delete requestParams['createFormNameUser'];
      if (requestParams?.createrID && requestParams?.createrID?.length > 0) {
        requestParams['createrID'] = requestParams?.createrID?.toString();
      } else {
        delete requestParams['createrID'];
      }

      delete requestParams['signFormNameDept'];
      delete requestParams['signFormNameUser'];
      if (requestParams?.signerID && requestParams?.signerID?.length > 0) {
        requestParams['signerID'] = requestParams?.signerID?.toString();
      } else {
        delete requestParams['signerID'];
      }

      delete requestParams['cancelFormNameDept'];
      delete requestParams['cancelFormNameUser'];
      if (requestParams?.cancelID && requestParams?.cancelID?.length > 0) {
        requestParams['cancelID'] = requestParams?.cancelID?.toString();
      } else {
        delete requestParams['cancelID'];
      }

      delete requestParams['applyFormNameDept'];
      delete requestParams['applyFormNameUser'];
      if (requestParams?.applyerID && requestParams?.applyerID?.length > 0) {
        requestParams['applyerID'] = requestParams?.applyerID?.toString();
      } else {
        delete requestParams['applyerID'];
      }

      run({
        ...requestParams,
      });
    },
    [],
  );

  /** 各类字典树 */
  let loadMapList = useMemo(() => {
    return {
      loadMdicMapList,
      searchList: getVersionManageSearchColumns(
        loadMdicMapList,
        manageTableParams,
      ),
    };
  }, [loadMdicMapList, manageTableParams]);

  /** 页面数据及分页信息 */
  const VersionDataInfo = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    loading,
    refreshRequest,
    manageTableParams,
    ...loadMapList,
    ...VersionDataInfo,
  };
};

export { VersionManageService };
